package com.keqiongzc.module_login

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.everyline.lib_common.bean.UserInfoBean
import com.tencent.mmkv.MMKV

/**
 * Created by on 2021/2/27.
 * 天天挂线
 *  746608431@qq.com
 *  描述:TODO
 */

object UserManager {
    private const val USER_DATA: String = "user_data"
    private var mmkv: MMKV = MMKV.defaultMMKV()!!

    private val liveData = MutableLiveData<UserInfoBean>()

    fun getLoginLiveData(): MutableLiveData<UserInfoBean> {
        return liveData
    }

    fun getUser(): UserInfoBean? {
        return mmkv.decodeParcelable(USER_DATA, UserInfoBean::class.java)
    }

    fun saveUser(user: UserInfoBean?) {
        mmkv.encode(USER_DATA, user)
        if (liveData.hasObservers()) {
            liveData.postValue(user!!)
        }
    }

    fun isLogin(): Boolean {
        return getUser() != null
    }

    fun removeUser() {
        mmkv.encode(USER_DATA, "")
//        if (liveData.hasObservers()) {
//            liveData.postValue(getUser())
//        }
    }

    fun start(context: Context): LiveData<UserInfoBean> {
        return liveData
    }

}